﻿using System;
using Autodesk.Revit.DB;
using YJKRevitKernel.XYZUtility;

namespace YJKRevitDimensionText.VerticalAutoDim.GeoModel
{
	internal abstract class WallGeo
	{
		protected Transform TransformWcs2Ecs { get; set; }

		protected Plane plane { get; set; }

		protected Wall Wall { get; set; }

		protected Line WallBaseClipLine { get; set; }

		protected XYZ Max3DPoint { get; set; }

		protected XYZ Min3DPoint { get; set; }

		protected XYZ Max2DPoint { get; set; }

		protected XYZ Min2DPoint { get; set; }

		public Plane Plane
		{
			get
			{
				return this.plane;
			}
		}

		public Element Element
		{
			get
			{
				return this.Wall;
			}
		}

		public int Weight
		{
			get
			{
				return 12;
			}
		}

		public XYZ Max2DPointECS
		{
			get
			{
				return this.Max2DPoint;
			}
		}

		public XYZ Min2DPointECS
		{
			get
			{
				return this.Min2DPoint;
			}
		}

		public XYZ Max3DPointWCS
		{
			get
			{
				return this.Max3DPoint;
			}
		}

		public XYZ Min3DPointWCS
		{
			get
			{
				return this.Min3DPoint;
			}
		}

		public double TopElev
		{
			get
			{
				return this.Max3DPoint.Z;
			}
		}

		public double BottomElev
		{
			get
			{
				return this.Min3DPointWCS.Z;
			}
		}

		public double Width
		{
			get
			{
				return this.WallBaseClipLine.Length;
			}
		}

		public double Height { get; private set; }

		public double Dist2Plane { get; private set; }

		public double Distance2Plane
		{
			get
			{
				return this.Dist2Plane;
			}
		}

		public WallGeo(Element elem, Line wallBaseClipLine, Plane pl, Transform transformWcs2Ecs)
		{
			this.Wall = (elem as Wall);
			this.plane = pl;
			this.WallBaseClipLine = wallBaseClipLine;
			this.TransformWcs2Ecs = transformWcs2Ecs;
		}

		public bool Init()
		{
			double naN = double.NaN;
			double naN2 = double.NaN;
			Utility.GetWallElevations(this.Wall, this.Plane, this.TransformWcs2Ecs, ref naN2, ref naN);
			this.Height = naN - naN2;
			XYZ xyz = this.WallBaseClipLine.GetEndPoint(0);
			XYZ xyz2 = this.WallBaseClipLine.GetEndPoint(1);
			xyz = new XYZ(xyz.X, xyz.Y, naN2);
			xyz2 = new XYZ(xyz2.X, xyz2.Y, naN2);
			if (XYZUtilityMethod.IsSameDirection(this.WallBaseClipLine.Direction, this.Plane.XVec))
			{
				this.Min3DPoint = xyz;
				this.Max3DPoint = xyz2 + this.Plane.YVec * this.Height;
			}
			else
			{
				this.Min3DPoint = xyz2;
				this.Max3DPoint = xyz + this.Plane.YVec * this.Height;
			}
			Transform transform = Transform.CreateReflection(this.Plane);
			XYZ xyz3 = transform.OfPoint(this.Min3DPoint);
			this.Min3DPoint = (xyz3 + this.Min3DPoint) * 0.5;
			XYZ xyz4 = transform.OfPoint(this.Max3DPoint);
			this.Max3DPoint = (xyz4 + this.Max3DPoint) * 0.5;
			this.Dist2Plane = xyz4.DistanceTo(this.Max3DPoint);
			this.Min2DPoint = this.TransformWcs2Ecs.OfPoint(this.Min3DPoint);
			this.Max2DPoint = this.TransformWcs2Ecs.OfPoint(this.Max3DPoint);
			return true;
		}
	}
}
